package com.five.calendar.function.schedule;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.five.calendar.R;
import com.five.calendar.base.BaseFragment;
import com.five.calendar.common.observer.Observer;
import com.five.calendar.database.bean.ScheduleBean;
import com.five.calendar.database.observer.ScheduleObservable;
import com.five.calendar.ui.adapter.ScheduleListAdapter;

import java.util.List;

/**
 * @desc:
 * @auther:duwei
 * @date:2017/8/30
 */

public class ScheduleFragment extends BaseFragment {
    private ListView mListView;
    private ScheduleListAdapter mAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.calendar_schedule, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mListView = (ListView) view.findViewById(R.id.schedule_list);
        ScheduleObservable.querySchedule().registerObserver(new Observer<ScheduleBean, Integer, List<ScheduleBean>>() {
            @Override
            public void onFail(Integer integer) {

            }

            @Override
            public void onComplete(List<ScheduleBean> scheduleBeans) {
                if(scheduleBeans != null){
                    mAdapter = new ScheduleListAdapter(scheduleBeans, ScheduleFragment.this.getContext());
                    mListView.setAdapter(mAdapter);
                }
            }

            @Override
            public void onSuccess(ScheduleBean scheduleBean) {

            }
        }).startDataHandle();
    }

    @Override
    public void onResume() {
        super.onResume();
        ScheduleObservable.querySchedule().registerObserver(new Observer<ScheduleBean, Integer, List<ScheduleBean>>() {
            @Override
            public void onFail(Integer integer) {

            }

            @Override
            public void onComplete(List<ScheduleBean> scheduleBeans) {
                if(scheduleBeans != null){
                    mAdapter = new ScheduleListAdapter(scheduleBeans, ScheduleFragment.this.getContext());
                    mListView.setAdapter(mAdapter);
                }
            }

            @Override
            public void onSuccess(ScheduleBean scheduleBean) {

            }
        }).startDataHandle();
    }
}
